import java.util.ArrayDeque;

// by Sacha Best and Brian Bagdasarian

public class Trader implements Comparable<Trader>
{
	private String name, password;
	private Brokerage brokerage;
	private ArrayDeque<String> messages;
	private TraderWindow myWindow;
	
	public Trader(Brokerage brokerage, String name, String password)
	{
		this.brokerage = brokerage;
		this.name = name;
		this.password = password;
		messages = new ArrayDeque<String>();
		myWindow = null;
	}
	public Trader()
	{
		this(null, null, null);
	}
	
	public String getName()
	{
		return name;
	}
	public String getPassword()
	{
		return password;
	}

	public boolean equals(Object other)
	{
		if (!(other instanceof Trader)) 
			throw new ClassCastException();
		Trader compare = (Trader) other;
		return getName().equalsIgnoreCase(compare.getName());
	}
	public int compareTo(Trader other)
	{
		return getName().compareTo(other.getName());
	}
	
	public void openWindows()
	{
		myWindow = new TraderWindow(this);
	}
	public boolean hasMessages()
	{
		return messages.size() != 0;
	}
	public boolean recieveMessage(String message)
	{
		messages.addFirst(message);
		if (myWindow == null) 
			return false;
		while (!messages.isEmpty()) 
			myWindow.showMessage(messages.removeFirst());
		return true;
	}
	
	public void openWindow()
	{
		myWindow = new TraderWindow(this); //don't know what the actual constructor is yet
	}
	public void getQuote(String symbol)
	{
		brokerage.getQuote(symbol, this);
	}
	public void quit()
	{
		myWindow = null;
		brokerage.logout(this);
	}
	public void placeOrder(TradeOrder order)
	{
		brokerage.placeOrder(order);
	}
}
